﻿using System.Diagnostics;
using System.Windows.Threading;
using Wix.Win8SetupBootstrapper.ViewModels;
using Wix.Win8SetupBootstrapper.Views;
using Microsoft.Tools.WindowsInstallerXml.Bootstrapper;

namespace Wix.Win8SetupBootstrapper
{
    public class WixBootstrapper : BootstrapperApplication
    {
        static public Dispatcher BootstrapperDispatcher { get; private set; }

        protected override void Run()
        {
#if DEBUG
            Debugger.Launch();
#endif
            base.Engine.CloseSplashScreen();
            
            this.Engine.Log(LogLevel.Verbose, "Launching Installation Interface");

            BootstrapperDispatcher = Dispatcher.CurrentDispatcher;

            var rootView = new RootView(this);
            rootView.Closed += delegate
            {
                BootstrapperDispatcher.InvokeShutdown();
            };

            this.Engine.Detect();

            rootView.Show();

            Dispatcher.Run();

            this.Engine.Quit(rootView.ViewModel.FinalResult);
        }
    }
}
